package com.ws.weatherapp

import android.view.LayoutInflater
import android.view.ViewGroup

import androidx.recyclerview.widget.RecyclerView
import com.ws.weatherapp.databinding.DetailedWeatherItemBinding
import models.DetailedWeatherModel

lateinit var binding: DetailedWeatherItemBinding
class DetailedWeatherAdapter(private val data: List<DetailedWeatherModel>): RecyclerView.Adapter<DetailedWeatherAdapter.ViewHolder>() {
    class ViewHolder(b: DetailedWeatherItemBinding): RecyclerView.ViewHolder(b.root){
        val time_tv = b.itemTimeTv
        val temp_tv = b.itemTempTv
        val weather_icon = b.itemWeatherIcon
        val rain_tv = b.itemRainTv
        val wind_tv = b.itemWindTv
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        binding = DetailedWeatherItemBinding.inflate(LayoutInflater.from(parent.context))
        return ViewHolder(binding)
    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        holder.temp_tv.text = "${data[position].temp}°"
        holder.rain_tv.text = "${data[position].rain_count} mm"
        holder.wind_tv.text = "${data[position].weather} m/s"
        holder.time_tv.text = data[position].time
        holder.weather_icon.setImageResource(
            when(data[position].weather){
                "Thunderstorm" -> R.drawable.ic_ic_weather_wind_storm
                "Drizzle" -> R.drawable.ic_ic_weather_drop
                "Clouds" -> R.drawable.ic_ic_weather_cloudy
                "Snow" -> R.drawable.ic_ic_weather_snow
                else -> R.drawable.ic_ic_weather_sunny
            }
        )
    }

    override fun getItemCount(): Int {
        return data.size
    }
}